package com.system.ad.index.district;


import com.system.ad.index.IndexAware;
import com.system.ad.search.vo.feature.DistrictFeature;
import com.system.ad.utlis.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.stream.Collectors;

/**
 * 地域索引服务
 */
@Slf4j
@Component
public class DistrictIndex implements IndexAware<String, Set<Long>> {


    private static Map<String, Set<Long>> districtUnitMap = new ConcurrentHashMap<>();
    private static Map<Long, Set<String>> unitDistrictMap = new ConcurrentHashMap<>();

    @Override
    public Set<Long> get(String key) {
        return districtUnitMap.get(key);
    }

    @Override
    public void add(String key, Set<Long> value) {

        log.info("unitDistrictIndex ,before add:{}", unitDistrictMap);
        // 同时添加到关键字索引和ID索引集合
        // 需要判断key是否存在，创建或直接添加
        Set<Long> unitIdSet = CommonUtils.getterCreate(key, districtUnitMap, ConcurrentSkipListSet::new);
        unitIdSet.addAll(value);

        // 循环插入推广单元Id
        for (Long unitId : value) {
            Set<String> its = CommonUtils.getterCreate(unitId, unitDistrictMap, ConcurrentSkipListSet::new);
            its.add(key);
        }
        log.info("unitDistrictIndex ,after add:{}", unitDistrictMap);

    }

    @Override
    public void update(String key, Set<Long> value) {
        log.error("District index cat not support update");
    }

    @Override
    public void delete(String key, Set<Long> value) {
        log.info("unitDistrictIndex ,before delete:{}", districtUnitMap);
        //取出需要删除部分
        Set<Long> unitIds = CommonUtils.getterCreate(key, districtUnitMap, ConcurrentSkipListSet::new);
        unitIds.removeAll(value);

        // 循环插入推广单元Id
        for (Long unitId : value) {
            Set<String> itTags = CommonUtils.getterCreate(unitId, unitDistrictMap, ConcurrentSkipListSet::new);
            itTags.remove(key);
        }
        log.info("unitDistrictIndex ,after delete:{}", districtUnitMap);
    }

    public boolean match(Long adUnitId,
                         List<DistrictFeature.ProvinceAndCity> districts) {

        if (unitDistrictMap.containsKey(adUnitId) &&
                CollectionUtils.isNotEmpty(unitDistrictMap.get(adUnitId))) {

            Set<String> unitDistricts = unitDistrictMap.get(adUnitId);

            List<String> targetDistricts = districts.stream()
                    .map(
                            d -> CommonUtils.stringConcat(
                                    d.getProvince(), d.getCity()
                            )
                    ).collect(Collectors.toList());

            return CollectionUtils.isSubCollection(targetDistricts, unitDistricts);
        }

        return false;
    }

    /**
     * 判断关键字索引是否命中
     *
     * @param unitId
     * @param itTags
     * @return
     */
//    public boolean match(Long unitId, List<String> itTags) {
//        // 判断索引中是否存在推广单元ID，并且推广关键字不为空
//        if (unitDistrictMap.containsKey(unitId) && CollectionUtils.isNotEmpty(unitDistrictMap.get(unitId))) {
//            Set<String> unitKeywords = unitDistrictMap.get(unitId);
//            //判断关键字是否存在集合中
//            return CollectionUtils.isSubCollection(itTags, unitKeywords);
//        }
//        return false;
//    }
}
